<template>
	<view class="login-container">
		<!-- 底部登录区域 -->
		<view class="login-section">
			<!-- 微信登录按钮 -->
			<view class="wechat-login-btn" @click="handleAuthCode">
				微信授权登录
			</view>
			<!-- 隐私政策同意选项 -->
			<view class="privacy-agreement">
				<checkbox-group @change="handleAgreementChange">
					<label class="agreement-label">
						<label class="radio">
							<radio value="r1" color="#529AEE" checked="true" />
							<text>已阅读并同意</text>
							<text class="policy-link" @click="openPrivacyPolicy">《隐私政策》</text>
							<text>和</text>
							<text class="policy-link" @click="openServiceAgreement">《会员服务协议》</text>
						</label>
					</label>
				</checkbox-group>
			</view>
		</view>
		<show-modal></show-modal>
	</view>
</template>

<script>
	/**
	 * 登录页面组件
	 * @description 提供微信授权登录功能和隐私政策同意选项
	 */
	import {appID,redirectUri} from "@/utils/config.js"
	export default {
		data() {
			return {
				/**
				 * 用户是否同意隐私政策和服务协议
				 * @type {Boolean}
				 */
				isAgreed: true,
				isOk: false
			}
		},
		onLoad() {
			let paramValue = this.getParameterByName('code');
			if(paramValue){
				this.handleOpenId(paramValue)
			}else{
				this.$api('/app/getAgreement','GET',{code:'privacy_policy'}).then(res=>{
					console.log(res.data)
					this.$showModal({
						title: '隐私政策',
						content: res.data.code==200?res.data.data:'',
						cancelText: "不同意并退出",
						confirmText: "同意",
						success: (res) => {
							if (res.confirm) {
								this.isAgreed = true
							} else if (res.cancel) {
								if (typeof WeixinJSBridge !== 'undefined') {
								    WeixinJSBridge.call('closeWindow')// 微信自带的关闭窗口WeixinJSBridge.call("closeWindow")
								  } else {
								    // H5 环境
								    if (navigator.userAgent.indexOf('MSIE') > 0) {
								      if (navigator.userAgent.indexOf('MSIE 6.0') > 0) {
								        window.opener = null
								        window.close()
								      } else {
								        window.open('', '_top')
								        window.top.close()
								      }
								    } else if (navigator.userAgent.indexOf('Firefox') > 0) {
								      window.location.href = 'about:blank '
								    } else {
								      window.opener = null
								      window.open('', '_self', '')
								      window.close()
								    }
								  }
							}
						}
					})
				})
			}
			
		},
		methods: {
			getParameterByName(name, url = window.location.href) {
			    name = name.replace(/[\[\]]/g, "\\$&");
			    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
			        results = regex.exec(url);
			    if (!results) return null;
			    if (!results[2]) return '';
			    return decodeURIComponent(results[2].replace(/\+/g, " "));
			},
			/**
			 * 处理微信授权登录
			 * @description 验证用户是否同意协议，然后进行微信授权登录
			 */
			handleAuthCode() {
				let currentPageUrl = redirectUri;
				let path = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${currentPageUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
				console.log(path)
				window.location.href = path
			},
			/**
			 * 处理协议同意状态变更
			 * @param {Event} e - 复选框变更事件
			 */
			handleAgreementChange(e) {
				this.isAgreed = e.detail.value.length > 0
			},
			// 发送后端
			async handleOpenId(code) {
				let {data:res} = await this.$api("/app/login","POST",{wxCode:code})
				// console.log(res)
				if(res.code==200){
					uni.setStorageSync('token',res.data.token)
					uni.reLaunch({
						url:"/pages/index/index"
					})
				}else{
					uni.showToast({
						title:res.msg,
						icon:"none"
					})
				}
               
			},
			/**
			 * 打开隐私政策页面
			 */
			openPrivacyPolicy() {
				uni.navigateTo({
					url: '/pages/parse/parse?title=隐私政策&code=privacy_policy'
				})
			},

			/**
			 * 打开服务协议页面
			 */
			openServiceAgreement() {
				uni.navigateTo({
					url: '/pages/parse/parse?title=会员服务协议&code=service_agreement'
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.login-container {
		width: 100vw;
		height: 100vh;
		position: relative;
		overflow: hidden;
		background-image: url("/static/0WlmEl0MYy.jpg");
		background-size: 120% 100%;
	}


	.login-section {
		width: 100vw;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: absolute;
		bottom: 30rpx;
	}

	.wechat-login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #529AEE;
		color: #FFFFFF;
		font-size: 28rpx;
		border-radius: 45rpx;
		margin-bottom: 200rpx;
		text-align: center;
	}

	.privacy-agreement {
		display: flex;
		justify-content: flex-start;
		font-size: 26rpx;
		color: #666666;
		margin-bottom: 20rpx;

	}

	.agreement-label {
		display: flex;
		align-items: center;
		color: #fff;

		.radio {
			display: flex;
			align-items: center;
		}
	}

	.policy-link {
		color: #529AEE;
		margin: 0 4rpx;
	}
</style>